package com.zyb.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyb.commonutils.JwtUtils;
import com.zyb.commonutils.R;
import com.zyb.commonutils.comment.UcenterMemberComment;
import com.zyb.eduservice.client.UcenterClient;
import com.zyb.eduservice.entity.EduComment;
import com.zyb.eduservice.service.EduCommentService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author testJava
 * @since 2022-04-13
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/comment")
public class EduCommentController {

    @Resource
    private EduCommentService commentService;
    @Resource
    private UcenterClient ucenterClient;

    //根据课程id查询评论列表
    @GetMapping("commentPage/{page}/{limit}")
    public R commentPage(@PathVariable Long page,@PathVariable Long limit,String courseId){
        Page<EduComment> pageParam = new Page<>(page, limit);

        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        commentService.page(pageParam,wrapper);
        List<EduComment> records = pageParam.getRecords();

        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", pageParam.getCurrent());
        map.put("pages", pageParam.getPages());
        map.put("size", pageParam.getSize());
        map.put("total", pageParam.getTotal());
        map.put("hasNext", pageParam.hasNext());
        map.put("hasPrevious", pageParam.hasPrevious());
        return R.ok().data(map);
    }

    //添加评论
    @PostMapping("addComment")
    public R addComment(@RequestBody EduComment comment, HttpServletRequest request){
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(userId)) {
            return R.error().code(28004).message("请登录");
        }

        comment.setMemberId(userId);
        UcenterMemberComment userInfo = ucenterClient.getUserInfo(userId);
        comment.setNickname(userInfo.getNickname());
        comment.setAvatar(userInfo.getAvatar());

        commentService.save(comment);
        return R.ok();
    }

    /*//评论查询带分页
    @GetMapping("getCommentPage/{page}/{limit}")
    public R getCommentPage(@PathVariable Long page, @PathVariable Long limit) {
        Page<EduComment> commentPage = new Page<>(page, limit);
        commentService.page(commentPage,null);
        List<EduComment> records = commentPage.getRecords();
        long total = commentPage.getTotal();
        return R.ok().data("records", records).data("total", total);
    }

    //添加评论
    @PostMapping("addComment")
    public R addComment(@RequestBody EduComment eduComment) {
        boolean flag = commentService.save(eduComment);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    //根据Id查询一条评论
    @GetMapping("getCommentById/{commentId}")
    public R getCommentById(@PathVariable String commentId) {
        EduComment comment = commentService.getById(commentId);
        return R.ok().data("comment", comment);
    }

    //修改评论
    @PostMapping("updateComment")
    public R updateComment(@RequestBody EduComment eduComment) {
        boolean flag = commentService.updateById(eduComment);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    //删除评论
    @DeleteMapping("deleteComment/{commentId}")
    public R deleteComment(@PathVariable String commentId) {
        boolean flag = commentService.removeById(commentId);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }*/
}

